package com.alex.controller.test.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor: " + "preHandler");



        // 如果session域中存储有用户信息,则允许通过
        if (request.getSession().getAttribute("adminLoginInfo") != null) {
            return true;
        }

        // 没有检测到管理员登录信息, 重定向到 admin/login 页面
        response.sendRedirect(request.getContextPath() + "/admin/login");

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("LoginInterceptor: " + "postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("LoginInterceptor: " + "afterCompletion");
    }
}
